﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace myBoard.App.Components.MagnetsStore
{
    public partial class Ratings
    {
        public static readonly DependencyProperty MaxRatingProperty = DependencyProperty.Register("MaxRating", typeof(int), typeof(Ratings),
            new PropertyMetadata(5, new PropertyChangedCallback(OnMaxRatingChanged)));

        public static readonly DependencyProperty RatingProperty = DependencyProperty.Register("Rating", typeof(int?), typeof(Ratings),
            new PropertyMetadata(null,new PropertyChangedCallback(OnRatingChanged)));

        public static readonly DependencyProperty IsReadOnlyProperty =
            DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(Ratings), new UIPropertyMetadata(true, IsReadOnlyChanged));


        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }


        public int MaxRating
        {
            get
            {
                return Convert.ToInt32(GetValue(MaxRatingProperty));
            }
            set
            {
                SetValue(MaxRatingProperty, value);
            }
        }

        public int? Rating
        {
            get
            {
                return Convert.ToInt32(GetValue(RatingProperty));
            }
            set
            {
                SetValue(RatingProperty, value);
            }
        }

        static void OnMaxRatingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            Ratings r = sender as Ratings;
            r.UpdateRating();
        }

        static void OnRatingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            Ratings r = sender as Ratings;
            if (e.NewValue == null)
                r._starsContainer.Children.Clear();
            else
                r.UpdateRating();
        }

        private static void IsReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        } 


        public Ratings()
        {
            this.InitializeComponent();

            // Insert code required on object creation below this point.
        }

        public void UpdateRating()
        {
            if (MaxRating >= Rating && MaxRating != 0)
            {
                _starsContainer.Children.Clear();

                for (int i = 0; i < MaxRating; i++)
                {
                    RatingStar rs = new RatingStar();
                    rs.Cursor = Cursors.Hand;
                    rs.MouseLeftButtonUp += (sender, e) =>
                    {
                        if (!IsReadOnly)
                        {
                            int rating = _starsContainer.Children.IndexOf((RatingStar)sender) + 1;
                            Rating = rating;
                        }
                    };

                    if (Rating > i)
                        rs.Active = true;
                    else
                        rs.Active = false;

                    _starsContainer.Children.Add(rs);
                }
            }
        }
    }
}
